package chap10collection.test25;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zdl
 * @date 2024/12/17
 */
class MyStack{
    private ArrayList<Object> list;

    public MyStack() {
        list = new ArrayList<>();
    }

    public MyStack(ArrayList<Object> list) {
        this.list = list;
    }
    //push(Object o)：表示把元素放入栈
    public void push (Object o){
        list.add(o);
    }
    //Object pop()：返回栈顶元素，并把该元素从栈中删除。如果栈为空，则返回 null 值
    public Object pop(){
        if (list.isEmpty()){
            return null;
        }
        return list.remove(list.size() - 1);
    }
    //Object peek()：返回栈顶元素，但不把该元素删除。如果栈为空，则返回 null 值
    public Object peek(){
        if (list.isEmpty()){
            return null;
        }
        return list.get(list.size() - 1);
    }
    //boolean isEmpty()：判断该栈是否为空
    public boolean isEmpty(){
      return   list.isEmpty();
    }
    //int size()：返回该栈中元素的数量
    public int size(){
        return list.size();
    }

    /**
     * 获取
     * @return list
     */
    public ArrayList<Object> getStack() {
        return list;
    }

    /**
     * 设置
     * @param list
     */
    public void setStack(ArrayList<Object> list) {
        this.list = list;
    }

    public String toString() {
        return "MyStack{list = " + list + "}";
    }
}
public class Demo25 {
    public static void main(String[] args) {
        MyStack stack = new MyStack();
        System.out.println(stack.peek()); //返回null
        stack.push(17);
        stack.push(5);
        stack.push(123);
        stack.push(25);
        stack.push(12); //[17,5,123,25,12)
        System.out.println(stack);
        System.out.println(stack.peek()); //返回栈顶 12
        System.out.println(stack.pop()); //删除栈顶元素
        stack.push(83);
        System.out.println(stack);
        System.out.println(stack.isEmpty());//false
        System.out.println(stack.size());//5

    }
}
